package com.gljx.web.controller.back.power;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.gljx.constant.Constant;
import com.gljx.util.CommonUtil;
import com.gljx.web.Entity.Handle;
import com.gljx.web.Entity.Menu;
import com.gljx.web.service.HandelServcie;
import com.gljx.web.service.MenuService;

@Controller
@RequestMapping("/handle")
public class HandleAction {

	private  Logger logger = Logger.getLogger(getClass());
	
	@Autowired
	private MenuService menuService;
	
	@Autowired
	private HandelServcie handelServcie;
	
	@RequestMapping("/look.action")
    public ModelAndView look() {
		ModelAndView mav=new ModelAndView();
		mav.setViewName("/backManage/power/handle_list");
		return mav;
	}
	
	@RequestMapping("/PhandleList.action")
	@ResponseBody
	public List<Menu> PmenuList() {
		return menuService.allSysMenu("nav");
	}
	/**
	 * 插入
	* @author LiXiang  @date 2017年11月1日 下午4:59:20
	 */
	@RequestMapping("/add.action")
	@RequiresPermissions("/handle/add.action")
	@ResponseBody
    public Map <String,Object > add(Handle handle){
		Map <String,Object > dataMap = new HashMap<String, Object>();
		dataMap.put("success",  handelServcie.insertSelective(handle));
		return dataMap;
	}
	/**
	 * 操作详情
	* @param id
	* @return
	* @author LiXiang  @date 2017年11月16日 上午11:19:36
	 */
	@RequestMapping("/handledata.action")
	@ResponseBody
    public Map <String,Object > userdata(String id) {
		Map <String,Object > dataMap = new HashMap<String, Object>();
		dataMap.put("handle", handelServcie.selectByPrimaryKey(Integer.valueOf(id)));
		return dataMap;
	}
	/**
	 * 修改操作
	* @param handle
	* @return
	* @author LiXiang  @date 2017年11月16日 上午11:19:43
	 */
	@RequestMapping("/update.action")
	@RequiresPermissions("/handle/update.action")
	@ResponseBody
    public Map <String,Object > update(Handle handle){
		Map <String,Object > dataMap = new HashMap<String, Object>();
		dataMap.put("success",  handelServcie.updateByPrimaryKeySelective(handle));
		return dataMap;
	}
	/**
	 * 分页查询
	* @param rows
	* @param page
	* @param handleName
	* @param pmenuName
	* @param fisDelete
	* @return
	* @author LiXiang  @date 2017年11月16日 上午11:19:58
	 */
	@RequestMapping("/list.action")
	@ResponseBody
    public Map <String,Object > list(String rows, 
									String page, 
									String handleName,		//操作名称
									String pmenuName,		//菜单类型
									String fisDelete){		//删除标记
		if(CommonUtil.isSpecialChar_strict(handleName,pmenuName)){
			Map <String,Object > dataMap = new HashMap<String, Object>();
			logger.error("##自定义异常##:"+SecurityUtils.getSubject().getPrincipal()+">>>非法输入");
			dataMap.put("rows",new ArrayList<>());
			dataMap.put("total",0);
			return dataMap;
		}
		fisDelete = CommonUtil.isConstant(fisDelete,"0","1");
		
		return handelServcie.selectBIMenu(rows, page, handleName, pmenuName, fisDelete);
	}
	@RequestMapping("/alllist.action")
	@ResponseBody
	public List<Map<String, Object>> alllist(String id,String name,String isDelete,String menuName) {
		if(id == null){
			id = "0";
		}
		return handelServcie.findHandle(menuName, Constant.Project_ID, name,isDelete);
	}
	
	@RequestMapping("/setdelete.action")
	@RequiresPermissions("/handle/setdelete.action")
	@ResponseBody
    public Map <String,Object > setdelete(String ids,String isDelete){
		Map <String,Object > dataMap = new HashMap<String, Object>();
		try {
			dataMap.put("success",handelServcie.updateHandelDelete(ids, isDelete));
		} catch (Exception e) {
			dataMap.put("erroe","");
			logger.error("#角色操作失败#---停用启用"+e);
		}
		return dataMap;
	}
}
